<template>
  <div class="device">
    <el-page-header @back="goBack" :content="title"> </el-page-header>
    <div class="content">
      <el-form
        ref="form"
        :model="form"
        label-width="120px"
        label-position="left"
      >
        <el-form-item
          label="装备模型名称"
          prop="templateId"
          :rules="[
            {
              required: true,
              message: '请输入',
              trigger: 'change',
            },
          ]"
        >
          <el-select
            v-model="form.templateId"
            placeholder="请选择"
            style="width: 400px"
          >
            <el-option
              v-for="item in templateList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="装备名称"
          prop="equipmentName"
          :rules="[
            {
              required: true,
              message: '请输入',
              trigger: 'change',
            },
          ]"
        >
          <el-input class="ipt" v-model="form.equipmentName"></el-input>
        </el-form-item>
        <el-form-item
          label="权重"
          prop="weight"
          :rules="[
            {
              required: true,
              message: '请输入',
              trigger: 'change',
            },
          ]"
        >
          <el-input class="ipt" v-model="form.weight"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="btn">
      <el-button type="primary" @click="changeSure">确定</el-button>
      <el-button @click="goBack">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getEquipList, getTemplateDetail, saveEquip } from "@/api/equip";
export default {
  props: {
    type: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      maxHeight: "calc(100vh - 444px)",
      title: "新增装备",
      form: {
        templateId: "",
        equipmentName: "",
        tableData: [],
      },
      options: [
        {
          value: 0,
          label: "值",
        },
        {
          value: 1,
          label: "范围",
        },
      ],
      templateList: [],
      list: [],
    };
  },
  mounted() {
    this.templateList = [];
    getEquipList({ type: this.type, belong: 2})
      .then((res) => {
        const { code, data } = res?.data;
        if (code == 200) {
          data.forEach((item) => {
            this.templateList.push({
              label: item.templateName,
              value: item.templateId,
            });
          });
        }
      })
      .catch((err) => {
        this.$message.error(err);
      });
  },
  methods: {
    selectnameOne(val) {
      getTemplateDetail({ templateId: val })
        .then((res) => {
          const { code, data, msg } = res?.data;
          if (code == 200) {
            // this.form.tableData = data;
            data.forEach((item) => {
              this.form.tableData.push({
                attributeName: item.attributeName,
                unit: item.unit,
                attributeValueType: this.attributeValueType(
                  item.attributeValueType
                ),
                num: item.values?.length == 1 ? item.values[0] : "",
                valOne: item.values?.length == 2 ? item.values[0] : "",
                valTwo: item.values?.length == 2 ? item.values[1] : "",
                attributeId: item.attributeId,
              });
            });
          } else {
            this.$message.error(msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    attributeValueType(val) {
      switch (val) {
        case "NUMERIC":
          return 0;
        case "RANGE":
          return 1;
        default:
          return "";
      }
    },
    goBack() {
      this.$emit("goBack");
    },
    changeSure() {
      this.list = [];
      // this.handleData(this.form.tableData);
      const data = {
        templateId: this.form.templateId,
        equipmentName: this.form.equipmentName,
        weight: this.form.weight,
        attributes: this.list,
      };
      this.$refs.form.validate((valid) => {
        if (valid) {
          saveEquip(data)
            .then((res) => {
              const { code, msg } = res?.data;
              if (code == 200) {
                this.$message.success(msg);
                this.$emit("changeEdit");
              } else {
                this.$message.error(msg);
              }
            })
            .catch((err) => {
              this.$message.error(err);
            });
        }
      });
    },
    handleData(data) {
      data.forEach((item) => {
        if (item.attributeValueType == 0) {
          this.list.push({
            attributeName: item.attributeName,
            unit: item.unit,
            attributeValueType: item.attributeValueType,
            values: [item.num],
            pid: item.attributeId,
          });
        } else {
          this.list.push({
            attributeName: item.attributeName,
            unit: item.unit,
            attributeValueType: item.attributeValueType,
            values: [item.valOne, item.valTwo],
            pid: item.attributeId,
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.btn {
  margin-top: 24px;
  text-align: right;
}
.content {
  margin-top: 24px;
}
:deep(.el-input).ipt {
  width: 400px;
}
</style>
